package com.musicsonar.android.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import com.musicsonar.android.utils.MusicSonarConstant;

public class SplashActivity extends Activity {

	private static SplashActivity me;

	private static int actualImage = R.id.imageSplash;

	private static Animation fadeIn;

	private static Animation fadeOut;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.splash);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

		me = this;
		
		if(MusicSonarActivity.mp != null){
			MusicSonarConstant.isValidated = Boolean.TRUE;
			finish();

			//start a new activity
			Intent i = new Intent();
			i.setClass(me.getBaseContext(), MusicSonarActivity.class);
			startActivity(i);
		}

		final ImageView splashView = (ImageView)findViewById(actualImage);
		if(splashView != null){
			splashView.setImageResource(R.drawable.splash);

			fadeOut = AnimationUtils.loadAnimation(me, R.anim.fadeout);
			fadeOut.setAnimationListener(new AnimationListener() {
				
				public void onAnimationStart(Animation animation) {
				}
				
				public void onAnimationRepeat(Animation animation) {
				}
				
				public void onAnimationEnd(Animation animation) {
					if(actualImage != R.drawable.splash_2){
						actualImage = R.drawable.splash_2;
						splashView.setImageResource(actualImage);
						splashView.startAnimation(fadeIn);
						return;
					}
					finish();

					//start a new activity
					Intent i = new Intent();
					i.setClass(me.getBaseContext(), MusicSonarActivity.class);
					startActivity(i);
				}
			});
			
			fadeIn = AnimationUtils.loadAnimation(me, R.anim.fadein);
			fadeIn.setAnimationListener(new AnimationListener() {
				
				public void onAnimationStart(Animation animation) {
				}
				
				public void onAnimationRepeat(Animation animation) {
				}
				
				public void onAnimationEnd(Animation animation) {
					splashView.startAnimation(fadeOut);
				}
			});

			splashView.startAnimation(fadeIn);
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		finish();

		//start a new activity
		Intent i = new Intent();
		i.setClass(me.getBaseContext(), MusicSonarActivity.class);
		startActivity(i);
		return true;
	}

}